local BillboardDefine = require("billboard.BillboardDefine")
local BRoleLogic = require("billboard.BRoleLogic")
local BUnionLogic = require("billboard.BUnionLogic")
local BJJCLogic = require("billboard.BJJCLogic")
local JjcLadderMiddle = require("jjcLadder.JjcLadderMiddle")

fields = {zhandouli=1,guajiID=1,lv=1,tower=1,combatHero=1,heroBag =1, lianyu = 1 , dragonCopy = 1, absAct = 1}
unionFields = {lv=1,zhandouli=1}

db = db or {}

-- 初始排行榜
function initBoard(boardType)
    local board = {}
	board.uuid2rank = {} 			-- 根据UUID获取名次		
	board.rank2data = {}			-- 根据名次获取DATA
	board.dirty = nil 				-- 是否改变过
	board.lastSortTime = nil 		-- 最近刷新时间
	board.boardType = boardType 	-- 类型
	db[boardType] = board
end

-- 启动后初始化
function initAfterStart()

    for boardType = 1, BillboardDefine.BOARDTYPE_CNT do
	    initBoard(boardType)
	end

    --中心服不处理
    if _G.is_middle == true then return end

	BRoleLogic.loadBoard()
    BUnionLogic.loadUnionBoard()
end

-- 初始玩家/公会排名data
function initRankData(rankData)
	rankData = rankData or {}
	rankData.value1 = 0
	rankData.value2 = 0
	rankData.time = 0
	rankData.uuid = nil
	rankData.roleBase = nil
	return rankData
end

-- 排序
local function updateRank(board)
	if not board then return end
	for uuid in pairs(board.uuid2rank) do
		board.uuid2rank[uuid] = nil
	end
	for rank, data in ipairs(board.rank2data) do
		board.uuid2rank[data.uuid] = rank
	end
end

-- 检查是否需要刷新(部分排行榜)
function checkNeedSort(board)
	local nowTime = os.time()
	local lastSortTime = board.lastSortTime or 0
	board.lastSortTime = nowTime
	if nowTime - lastSortTime > BillboardDefine.SORTLAST_TIME then
		return true
	end
end

-- 刷新上榜时间
function refreshTime(boardType, db)
	db.billboard = db.billboard or {}
	db.billboard[boardType] = os.time()
end

-- 获取上榜时间
function getTime(boardType, db)
	if not db.billboard then
		return
	end
	return db.billboard[boardType]
end

-- 获取排名名次
function getRank(boardType, uuid)
    local board = getBoard(boardType)
    if not board then return end
    return board.uuid2rank[uuid]
end

-- 根据名次获取信息
function getRankData(boardType, rank)
    local board = getBoard(boardType)
    if not board then return end
    return board.rank2data[rank]
end

-- 获取
function getBoard(boardType, nosort)
	local board = db[boardType]
	if boardType == BillboardDefine.TYPE_JJC and checkNeedSort(board) then
		BJJCLogic.loadJJCBoard()
	end
	if board and board.dirty and (not nosort) then
		board.dirty = nil
		updateRank(board)
	elseif boardType == BillboardDefine.TYPE_JJCLADDER_M then
		if _G.is_middle then
			return JjcLadderMiddle.getBoard()
		end
	end
	return board
end

-- 公会解散 从公会排行榜中把 公会去除
function dismissUnion(unionUuid)
    local board = getBoard(BillboardDefine.TYPE_UNION)
    if not board then return end
    local rank = board.uuid2rank[unionUuid]
	if rank and rank > 0 and board.rank2data[rank] then
		table.remove(board.rank2data , rank )
		board.uuid2rank[unionUuid] = nil
		updateRank(board)
	end   
end

